import 'package:flutter/material.dart';

class ScaleAnimation extends StatefulWidget {
  @override
  _ScaleAnimationState createState() => _ScaleAnimationState();
}

class _ScaleAnimationState extends State<ScaleAnimation> with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _animationController;

  @override
  void initState() {
    _animationController = AnimationController(duration: Duration(seconds: 3), vsync: this);

    _animation = CurvedAnimation(parent: _animationController, curve: Curves.bounceIn);

    _animation = Tween(begin: 0.0, end: 400.0).animate(_animation);
    start();
    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  void scale() {
    _animationController.reverse();
  }

  void start() {
    _animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animation'),
      ),
      body: ListView(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                child: Text('开始'),
                onPressed: () {
                  start();
                },
              ),
              RaisedButton(
                child: Text('缩小'),
                onPressed: () {
                  scale();
                },
              ),
            ],
          ),
          _AnimationImage(
            animation: _animation,
          )
        ],
      ),
    );
  }
}

class _AnimationImage extends AnimatedWidget {
  final Animation<double> animation;

  _AnimationImage({Key key, @required this.animation})
      : assert(animation != null),
        super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animation,
      builder: (BuildContext ctx, Widget child) {
        return Image.network(
          'http://www.hinews.cn/pic/0/17/48/20/17482099_983978.jpg',
          width: animation.value,
          height: animation.value,
        );
      },
    );
  }
}
